package com.sti.platform.service.impl;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.aisino.core.server.BaseService;
import com.sti.platform.dao.RoleDao;
import com.sti.platform.model.Role;
import com.sti.platform.model.UserRole;
import com.sti.platform.service.RoleService;

@Service
public class RoleServiceImpl extends BaseService implements RoleService {
	
	@Autowired
	private RoleDao roleDao;
	

	@Override
	public List<Role> queryRole(String params,Integer index,Integer row) {
		if(index==null || index==0){//当前页数
			index=1;
		}
		if(row==null || row==0){//每页多少条
			row=10;
		}
		return roleDao.queryRole(params,index, row);
	}


	@Override
	public void delectRole(String roleId) {
		List<UserRole> glbs = roleDao.queryUserRole(roleId);
		if(glbs.size()>0){
			for(UserRole glb :glbs){
				super.remove(UserRole.class,glb.getId());
			}
			
		}
		super.remove(Role.class, roleId);
	}


	@Override
	public void delectRoles(String roleIds) {
		List<String> roles = Arrays.asList(roleIds.split(","));
		for(String role :roles){
			this.delectRole(role);
		}
	}


	@Override
	public void updateStatu(String roleId,Integer status) {
		Role role = super.get(Role.class, roleId);
		role.setStatus(status);
		super.save(role);
	}


	@Override
	public void updateStatus(List<String> list,Integer status) {
		for(String li :list){
			Role role = super.get(Role.class, li);
			role.setStatus(status);
			super.save(role);
		}
		
	}


	@Override
	public void updateRole(Role role) {
		super.update(role);
	}


	@Override
	public void addRole(Role role) {
		super.save(role);
	}


	@Override
	public Role queryRole(String roleId) {
		Role role = super.get(Role.class, roleId);
		return role;
	}

}
